﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AsyncTask : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string qq = this.TextBox1.Text;

        cn.com.webxml.webservice.qqOnlineWebService service
            = new cn.com.webxml.webservice.qqOnlineWebService();

        PageAsyncTask task = new PageAsyncTask(
                (Object source, EventArgs args, AsyncCallback cb, Object extraData)
            =>
                {
                    DateTime begin = DateTime.Now;
                    this.Context.Items["Begin"] = begin;
                    return service.BeginqqCheckOnline(qq, cb, service);
                },

        (IAsyncResult ar) =>
        {
            cn.com.webxml.webservice.qqOnlineWebService sourceService = ar.AsyncState
                as cn.com.webxml.webservice.qqOnlineWebService;
            string code = sourceService.EndqqCheckOnline(ar);

            DateTime begin = (DateTime)this.Context.Items["Begin"];
            DateTime end = DateTime.Now;
            TimeSpan span = end - begin;

            this.Label1.Text = QQOnlineHelper.GetOnlineStatus(code);
            this.Label2.Text = string.Format("请求共花费：{0} 毫秒。", span.TotalMilliseconds);
        },
        (IAsyncResult ar) =>
        {
            this.Label1.Text = "处理超时";
        },
            null
            );



        this.RegisterAsyncTask(task);

    }

    protected void Page_PreRender(object sender, EventArgs e)
    {

    }

    protected void Page_PreRenderComplete(object sender, EventArgs e)
    {

    }
}